package com.sm.li.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {


    /**
     * 对象转字符串
     * @param obj
     * @return
     */
    public static String obj2Str(Object obj){
        try {
            ObjectMapper objectMapper = new ObjectMapper();
           String s = objectMapper.writeValueAsString(obj);
            return s;
        } catch (Exception e) {
          return "";
        }
    }

    public static <T> T str2Obj(String jsonStr,Class<T> tClass){
        try {
            ObjectMapper objectMapper = new ObjectMapper();
           T t = objectMapper.readValue(jsonStr,tClass);
            return t;
        } catch (Exception e) {
            return null;
        }
    }
}
